<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible=true">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="280" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="200" prop="phonenumber" />
        <el-table-column label="角色" width="200" prop="roleName" />
        <el-table-column label="状态" width="120">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="350">
          <template #default="scope">
            <el-button v-permission="'sys:user:add_edit'" size="mini" type="text" @click="updateEmployee(scope.row.id)">编辑</el-button>
            <el-button v-permission="'sys:user:remove'" size="mini" type="text" @click="delEmployee(scope.row.id)">删除</el-button>
            <el-button v-permission="'sys:user:resetPwd'" size="mini" type="text" @click="resetPassword(scope.row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :page-size="params.pageSize"
        :total="total"
        layout="total, prev, pager, next"
        @current-change="pageChange"
      />
    </div>
    <!-- 添加员工 -->
    <edit-employee :user-id="userId" :dialog-visible.sync="dialogVisible" @getEmployeeList="getEmployeeList" />
  </div>
</template>

<script>
import { getEmployeeListAPI, delEmployeeAPI, resetEmployeePasswordAPI } from '@/api/System/employee'
import editEmployee from './components/AddOrUpdateEmployee.vue'
export default {
  name: 'Employee',
  components: {
    editEmployee
  },
  data() {
    return {
      employeeList: [],
      params: {
        page: 1,
        pageSize: 2,
        name: '' // 员工姓名
      },
      total: 0,
      dialogVisible: false,
      userId: null
    }
  },
  mounted() {
    this.getEmployeeList()
  },
  methods: {
    // 获取员工列表
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 查询按钮
    search() {
      this.params.page = 1
      this.getEmployeeList()
    },
    // 编辑按钮
    updateEmployee(id) {
      this.dialogVisible = true
      //  向子组件传递id
      this.userId = id
    },
    // 删除员工
    async delEmployee(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        this.$message.success('删除员工成功')
        this.getEmployeeList()
      })
    },
    // 重置密码
    resetPassword(id) {
      this.$confirm('确定将密码重置为“123456?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await resetEmployeePasswordAPI(id)
        this.$message.success('重置密码成功')
      })
    },
    // 分页逻辑
    pageChange(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    // 状态逻辑适配
    formatStatus(status) {
      const statusMap = {
        0: '禁用',
        1: '启用'
      }
      return statusMap[status]
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
